<?php
/**
 * 
 * @author chenqi
 * @uses 申请小鸽
 * @date 2015-07-10
 *
 */
class Cash extends WX_Controller{
   public function __construct()
	{
		parent::__construct();
		$this->load->model('cash_model');
		
	}
        /**
         * 提现列表
         */
        public function  index(){
            $cash=$this->cash_model->getCashAll(array('cash.user_id'=>$this->user['id']));
            $this->data['site_title']='提现记录';
            $this->data['cash']=$cash;
            $this->load->view('/wap/historical_money',$this->data);
        }
        
        public function add_page() {
            $this->data['site_title']='提现';
            $this->load->model('user_model');
            $user=$this->user_model->checkUser(array('user.id'=>$this->user['id']));
            $this->data['user']=$user;
            $this->data['kebalance']=$user['balance']-($user['balance']*0.1);
            $this->load->view('/wap/i_record',$this->data);
        }
         /**
     * 验证二级密码
     */
    public function check_password(){
        $message=false;
        if(empty($_POST['username']) || empty($_POST['password'])){
            $message=false;
            
        }else{
            $this->load->model('user_model');
            $user=$this->user_model->checkUser(array('username'=>$_POST['username']),'username,password2');
            if(empty($user)){
                $message=false;
            }else{
                if($user['password2']==md5($_POST['password'])){
                    $message=true;
                }
            }
        }
        echo json_encode($message);
    }
    /**
     * 添加申请提现记录
     */
    public function add(){
        $message=false;
        $_POST['username']=$this->user['username'];
            if(empty($_POST['username']) || empty($_POST['password'])){
            $message='参数不完整';
            
        }else{
            $this->load->model('user_model');
            $user=$this->user_model->checkUser(array('username'=>$_POST['username']),'id,username,password2,true_name,money,freeze,balance,tixian,tax,feiyong,bank_name,bank_account');
            if(empty($user)){
                $message='用户不存在';
            }else{
				$balance=$user['balance']-$_POST['money'];
                $is_success=true;
                   if($user['password2']!=md5($_POST['password'])){
                        $message='交易密码错误';
                        $is_success=false;
                   }else if(!$user['bank_name'] || !$user['bank_account']){
                       $message='请在会员列表编辑银行信息';
                       $is_success=false;
                   }else if($balance < 0 ){
					   $message='余额不足';
                       $is_success=false;
				   }
				   
                if( $is_success ){
                    $tax=$_POST['money']*0.08;
                    $feiyong=$_POST['money']*0.02;
                    $shiji_money=$_POST['money']-($tax+$feiyong);
                    $data['user_id']=$user['id'];
                    $data['money']  =$_POST['money'];
                    $data['bank_name']  =$user['bank_name'];
                    $data['bank_account']  =$user['bank_account'];
                    $data['freeze']  =$_POST['money'];
                    $data['status'] =0;
                    $data['create_time']=time();
                    $data['last_time']  =time();
                    $data['operation']  =$this->user['username'];
                    
                    $data['balance']    =$user['balance']-$_POST['money'];
                    $data['shiji_money']=$shiji_money;
                    $data['tax']=$tax;
                    $data['feiyong']=$feiyong;
                    $result=$this->cash_model->add($data);
                    if($result){
                        $update_data['balance']=$data['balance'];
                        $update_data['freeze']=$user['freeze']+$_POST['money'];
                        $update_data['tixian']=$user['tixian']+$_POST['money'];
                        $update_data['tax']=$user['tax']+$tax;
                        $update_data['feiyong']=$user['feiyong']+$feiyong;
                        $update=$this->user_model->editUser(array('id'=>$user['id']),$update_data);
                        $update=true;
                        if($update){
                            $logdata[]=array(
                                'user_id'=>$user['id'],
                                'username'=>$user['username'],
                                'true_name'=>$user['true_name'],
                                'money'=>'-'.$shiji_money,
                                'type' =>'tixian',
                                'remark'=>$user['true_name'].'您于'.date('Y-m-d H:i:s',time())."申请提现".$_POST['money']."元，扣除税费手续费".($feiyong+$tax)."实际到账".$shiji_money.'元',
                                'create_time'=>  time()
                            );
                           
                            $logdata[]=array(
                                'user_id'=>$user['id'],
                                'username'=>$user['username'],
                                'true_name'=>$user['true_name'],
                                'money'=>'-'.($feiyong+$tax),
                                'type' =>'feiyong',
                                'remark'=>$user['true_name'].'您本次提现产生了'.($feiyong+$tax).'管理费已冻结',
                                'create_time'=>  time()
                            );
                            $this->add_money_log($logdata);
                            
                        }else{
                            $this->cash_model->deleteCash(array('id'=>$result));
                            $is_success=false;
                            $message='提现失败，请稍后重试';
                        }
                    }
                }
            }
        }
        echo json_encode(array('success'=>$is_success,'msg'=>$message));
    }

        
	//获取城市联动
	private function city($parentno=0,$arealevel=1){

		$where["parentno"] = $parentno;
		$where["arealevel"] = $arealevel;
		
		$count=$this->area_model->getCount($where);

		if($count)
			$province = $this->area_model->getArea($where,$count,0,"sort ASC");
		
		return $province;
	}
	//城市联动接口
	public function area(){
		$parentno = $this->input->get('parentno');
		$arealevel= $this->input->get('arealevel');
		$prov=$this->city($parentno,$arealevel);
		echo json_encode($prov);
	}
	
	//提交申请
	public function submit(){
		$userid = $this->user['id'];
		//判断是否已申请		
		$name = $this->input->get('name');
		$phone = $this->input->get('phone');
		$identy = $this->input->get('identy');
		$area = $this->input->get('area');
		$area_no = $this->input->get("area_no");//地区编码
		$service = $this->input->get('service');
		$service_value = $this->input->get('service_value');
		$service_id = $this->input->get('service_id');
		$ss = array();
		foreach ($service_id as $k=>$v){
			$ss[$v] = $service_value[$k];
		}
		$ss = serialize($ss);
		
		$areas["detail"] = $area;
		$areas["area_no"] = $area_no;
		
		$data = array(
				"user_id"=>$userid,
				"server_card"=>$identy,
				"name"=>$name,
				"phone"=>$phone,
				"classes"=>$ss,
				"areas"=>serialize($areas),
				"server_flag"=>1,
				"check_flag"=>2,
				"update_time"=>$this->current_time,
				"create_time"=>$this->current_time
				);
		
		$this->load->model('master_model');
		
		$res = $this->master_model->addMaster($data);
		if($res){
			$this->response(array('status'=>true, 'data'=>'申请成功！'), 200);
		}
		$this->response(array('status'=>false, 'data'=>'预约出错，请重试！'));
		
	}
 
 	

}